package pers.jing.android.helper.page

import androidx.lifecycle.ViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import pers.jing.android.helper.model.STATE_DIALOG_CONFIRM
import pers.jing.android.helper.model.STATE_DIALOG_NONE
import pers.jing.android.helper.model.UIDialogState
import pers.jing.android.helper.model.UIProgressDialogState

/**
 * ViewModel基类
 * 用于管理常用的UI状态
 */
open class BaseViewModel: ViewModel(){
    /**
     * 弹窗状态, 默认页面只维护一个弹窗, 如果需要实现弹窗叠加, 需要在子类额外自定义弹窗状态
     */
    private val _dialogState = MutableStateFlow(UIDialogState())
    val dialogState = _dialogState.asStateFlow()

    /**
     * 进度弹窗状态, 默认页面只维护一个弹窗, 如果需要实现弹窗叠加, 需要在子类额外自定义弹窗状态
     */
    private val _progressDialogState = MutableStateFlow(UIProgressDialogState())
    val progressDialogState = _progressDialogState.asStateFlow()

    /**
     * 显示加载进度弹窗
     */
    fun showProgressDialog(msg: String, progress: Int = 0) {
        _progressDialogState.update {
            it.copy(show = true, msg = msg, progress = progress)
        }
    }

    /**
     * 关闭加载进度弹窗
     */
    fun closeProgressDialog() {
        _progressDialogState.update {
            it.copy(show = false)
        }
    }

    fun showConfirmDialog(msg: String, nextStep: String? = null) {
        _dialogState.update {
            it.copy(STATE_DIALOG_CONFIRM, msg, nextStep)
        }
    }

    fun closeConfirmDialog() {
        _dialogState.update {
            it.copy(STATE_DIALOG_NONE)
        }
    }


}